﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace RememberLib
{
    public class MyBookList
    {
        static MyBookList _MyBookList = null;
        public static MyBookList Instance()
        {
            if (_MyBookList == null)
            {
                _MyBookList = new MyBookList();
            }

            return _MyBookList;

        }

        RememberEntities context = new RememberEntities();
        public List<MyBookModel> GetMyBookList(string loginID)
        {
            var list = from b in context.MyBook
                    join c in context.CategoryList
                    on b.CategoryID equals c.CategoryID
                       select new MyBookModel { LoginID = b.LoginID, CategoryID=c.CategoryID ,  CategoryName = c.CategoryName, CategoryDesc = c.CategoryDesc, ItemTotal = (c.ItemTotal==null?0:c.ItemTotal.Value) , ItemDone = b.ItemDone, ItemDrill = b.ItemDrill, ItemPlan = b.PlanTotal, Status = b.Status };

            //var list = context.CategoryList.Where(c => c.CategoryLevel == (int)CategoryLevel.Book  );

            if ( !string.IsNullOrEmpty ( loginID ))
            {
                list = list.Where(c => c.LoginID == loginID);
            }
            return list.ToList ();
        }

        //public List<MyBookModel> GetBookList(string loginID)
        //{
        //    var list = from l in context.CategoryList 
        //                j join
        //               on b.CategoryID equals c.CategoryID
        //               select new MyBookModel { LoginID = b.LoginID, CategoryName = c.CategoryName, CategoryDesc = c.CategoryDesc, ItemTotal = (c.ItemTotal == null ? 0 : c.ItemTotal.Value), ItemDone = b.ItemDone, ItemDrill = b.ItemDrill, ItemPlan = b.PlanTotal, Status = b.Status };

        //    //var list = context.CategoryList.Where(c => c.CategoryLevel == (int)CategoryLevel.Book  );

        //    if (!string.IsNullOrEmpty(loginID))
        //    {
        //        list = list.Where(c => c.LoginID == loginID);
        //    }
        //    return list.ToList();
        //}


        public void SaveMyBook(CategoryList category, int status)
        {
            if (category == null)
                return;

            //int itemID = _Item
            string loginID = ""; //&& c.LoginID == loginID
            var list = context.MyBook.Where(c => c.CategoryID == category.CategoryID);

            MyBook mybook = null;
            if (list != null && list.Count() > 0)
            {
                mybook = list.First();
            }
            if (mybook == null)
            {
                mybook = new MyBook
                {
                    MyBookID = Guid.NewGuid (),
                    CategoryID = category.CategoryID,
                    LoginID = loginID,
                    StartDate = DateTime.Now,
                    ItemDone = 0,
                    ItemDrill = 0,
                    PlanTotal = 0,
                    Status = status,
                    PlanUnit = 0,
                    TodayFlow = "",
                    TodayStatus = 0,
                    OrderNumber = 1,
                    LastDate = DateTime.Now
                };

                context.AddToMyBook(mybook);
            }
            else
            {
                mybook.Status = status;
            }
            int rc = context.SaveChanges();

        }



    }
}
